-- share
-- Created by chengb Nov/5/2015
-- 成就：分享

-- 内部函数声明
local whenShare;

-- 注册facebook分享成功的回调
EventMgr.register("gamecenter_achievement/share", event.FB_SHARE_SUCC, function(args)
    whenShare(args);
end);

-- 关注line分享成功事件
EventMgr.register("gamecenter_achievement/share", event.LINE_SHARE_SUCC,  function(args)
    whenShare(args);
end);

-- 关注微信分享成功事件
EventMgr.register("gamecenter_achievement/share", event.WECHAT_SHARE_SUCC,  function(args)
    whenShare(args);
end);

-- 关注微博分享成功事件
EventMgr.register("gamecenter_achievement/share", event.WEIBO_SHARE_SUCC,  function(args)
    whenShare(args);
end);

-- 分享成功的回调
whenShare = function(args)
    -- 判断是否是符合要求的成就
    local function isValidId(id)
        if true == GameCenterAchievementM.isCompleted(id) then
            -- 已经完成了
            return false;
        end

        return true;
    end

    -- 遍历所有的子规则
    local ids = GameCenterAchievementM.getIdsByRule("share");
    for _, id in pairs(ids) do
        if isValidId(id) then
            -- 尝试完成成就
            GameCenterAchievementM.tryComplete(id, 1);
        end
    end
end

